package ServerStuff;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.sql.Date;
import java.util.Calendar;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;
import org.json.JSONObject;

import DataStorageClasses.Employee;
import DataStorageClasses.TimeSheet;
import Database.DatabaseConnector;

@WebServlet("/GetTimeSheetInfoServlet")
public class GetTimeSheetInfoServlet extends HttpServlet
{
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) 
	{
		JSONObject result = new JSONObject();
		
		try {
			String business = (String) request.getSession().getAttribute("username");
			if (business != null) {
				DatabaseConnector db = new DatabaseConnector();
				long jobID = Long.parseLong(request.getParameter("jobID"));
				long empID = Long.parseLong(request.getParameter("empID"));
				Date date;
				Calendar calendar = Calendar.getInstance();
				String[] dateString = request.getParameter("date").split("-");
				calendar.set(Integer.parseInt(dateString[0]), Integer.parseInt(dateString[1])-1, Integer.parseInt(dateString[2]));
				date = new Date(calendar.getTimeInMillis());
				
				TimeSheet timeSheet= db.getTimeSheet(jobID, empID, date);
				Employee employee = db.getEmployee(empID, business);
				try {
					result.put("timeSheet", timeSheet.getJSONObject());
					result.put("employee", employee.getJSONObject());
					response.getWriter().print(result);
				} catch (JSONException e) {
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (ArrayIndexOutOfBoundsException e) {
			e.printStackTrace();
		}
		
		response.setStatus(HttpURLConnection.HTTP_ACCEPTED);
	}
}
